<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Nilai extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_nilai');
		//$this->load->model('mdl_fakultas');
		//$this->load->model('mdl_prodi');
		//$this->output->enable_profiler(true);
	}
	
	function index(){
		$this->load->view('nilai');
	}
	
	
	function grid(){
		$data = $this->mdl_nilai->getdata2();
		echo $this->mdl_nilai->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_input(){
		$data = $this->mdl_nilai->getdata_input();
		echo $this->mdl_nilai->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_edit(){
		$data = $this->mdl_nilai->getdata();
		echo $this->mdl_nilai->togrid($data['row_data'], $data['row_count']);
	}
	
	function save(){
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		//var_dump($_POST['data_nilai']);
		
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		$cekdate =  date('Y-m-d H:i:s');
		$tgl_nilai = $data['data_nilai']['rows'][0]['TglNilai'];
		//if($cekdate <= $tgl_nilai){
		$result = $this->mdl_nilai->Update_Nilai($data);
		/*}
		else{
			$data['pesan_error']='Waktu Penilaian Telah Habis'; 
		}*/
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function add2(){		
		$this->load->view('nilai_form2');
	}
	
	function edit(){		
		$this->load->view('nilai_perbaikan');
	}
	
	//laporan
	function laporan_pdf($kd_prodi,$kd_matakuliah,$kd_dosen,$kd_kelas){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('P', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		$fil['kd_prodi'] = $kd_prodi;
		$fil['kd_matakuliah'] = $kd_matakuliah;
		$fil['kd_dosen'] = $kd_dosen;
		$fil['kd_kelas'] = $kd_kelas;
		
		 //$data['nama'] = 'indriyanto';
		 $data['namaUniv'] = 'UNIVERSITAS OTTOW GEISSLER';
		 $data['alamatUniv'] = 'Jln. Perkutut Kotaraja, Telp.(0967)581562';
		 $data['kotaUniv'] = 'Jayapura, Papua';
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_nilai->get_pdf($fil);
		
		
		/* if (count($da['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		} */
		
		
		 $konten = $this->load->view('nilai_laporan',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('exemple00.pdf');
	}
	
	function pdf($kd_prodi,$kd_matakuliah,$kd_dosen,$kd_kelas){
		# inisialisasi library
		$this->load->library('our_pdf');
		$this->our_pdf->FPDF('L', 'mm', 'A4');
		define('FPDF_FONTPATH',APPPATH."libraries/fpdf/font/");
		
		#get filter
		$fil['kd_prodi'] = $kd_prodi;
		$fil['kd_matakuliah'] = $kd_matakuliah;
		$fil['kd_dosen'] = $kd_dosen;
		$fil['kd_kelas'] = $kd_kelas;
		
		
		// ambil data dari tabel
		$pdfdata = $this->mdl_nilai->get_pdf($fil);
		
		if (count($pdfdata['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		}

		
		# create pdf
		$this->our_pdf->Open();
		$this->our_pdf->addPage();
		
		#LOGO HEADER
		$this->our_pdf->Image('asset/images/logo.png',10,3);
		//$this->our_pdf->Cell(110,9,'UNIVERSITAS OTTOW GEISSLER PAPUA',0,0,'C');
		
		// judul laporan
		//$this->our_pdf->setFont('arial','B',18);
		$posY = 35;
		$posX = 10;
		$midX = 150;
		$title = 'DAFTAR NILAI MAHASISWA';
		
		// judul laporan
		$this->our_pdf->setFont('arial','B',10);
		/*$posY = 11;
		$posX = 10;
		$midX = 150;
		$title = 'DAFTAR NILAI';*/
		$namas = ($fil['kd_matakuliah']!='')?$this->mdl_nilai->getNameByKodeMK($fil['kd_matakuliah']):'';
		$dosens = ($fil['kd_dosen']!='')?$this->mdl_nilai->getNameByKoDosen($fil['kd_dosen']):'';
		//$prodis = ($fil['kd_prodi']!='')?$this->mdl_khs->getProdiByKode($fil['kd_prodi']):'';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title) / 2);
		$this->our_pdf->text($textPosX,$posY,$title);
		$posY += 6;
		$namas = 'Matakuliah    : '.$namas;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($namas) / 2);
		$this->our_pdf->text($posX,$posY,$namas);
		$posY += 6;
		$dosens = 'Dosen           : '.$dosens;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($dosens) / 2);
		$this->our_pdf->text($posX,$posY,$dosens);
		$posY += 6;
		$kelass = 'Kelas	             : '.$kd_kelas;
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($kelass) / 2);
		$this->our_pdf->text($posX,$posY,$kelass);
		
		# setting coloumn
		$this->our_pdf->setFont('Arial','B',9);
		$this->our_pdf->setFillColor(255,255,255);
		$posY += 6;
		$this->our_pdf->setXY($posX,$posY);
		
		// init column
		$col_width = array();
		$col_align = array();
		$col_caption = array();
		$col_val_align = array();
		foreach($pdfdata['column'] as $col){
			array_push($col_width, 		$col['width']);
			array_push($col_align, 		$col['align']);
			array_push($col_caption, 	$col['caption']);
			array_push($col_val_align, 	$col['val_align']);
		}
		
		// set column option
		$this->our_pdf->SetWidths($col_width);
		$this->our_pdf->SetAligns($col_align);
		$this->our_pdf->Row($col_caption);
		
		# setting row
		$posY = 39;
		$this->our_pdf->setFont('arial','',9);	
		$this->our_pdf->setFillColor(255,255,255);
		
		$this->our_pdf->SetAligns($col_val_align);
		
		// insert record
		foreach($pdfdata['row'] as $row){
			$this->our_pdf->Row($row);
		}
		
		// finish
		$this->our_pdf->AliasNbPages();
		$this->our_pdf->Output("Data Nilai.pdf", "I");
	}
}